package com.hq;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class CheckMd {

    private static String md_div = "md_div";
    private static String md_div_last = "md_div_last";
    private static String md_itemprop_knowledge = "md_itemprop_knowledge";
    private static String md_itemprop_what = "md_itemprop_what";
    private static String md_itemprop_how = "md_itemprop_how";
    private static String md_itemprop_where = "md_itemprop_where";

    private static String md_greater_than = "md_greater_than";
    private static String md_less_than = "md_less_than";

    public static void main(String[] args) {
        String fileName = "E:\\IdeaProjects\\JavaDesignDemo\\aa.txt";
        String str = readFileContent(fileName);
        checkMdTag(str);
    }

    public static void checkMdTag(String str) {

        System.out.println("开始----------------------------------------------");
        System.out.println(str);

        System.out.println("替换----------------------------------------------");
        Map<String, String> map = new LinkedHashMap<>();
        map.put(md_div, "<div");
        map.put(md_div_last, "</div>");
        map.put(md_itemprop_knowledge, "itemprop=\"knowledge\">");
        map.put(md_itemprop_what, "itemprop=\"what\">");
        map.put(md_itemprop_how, "itemprop=\"how\">");
        map.put(md_itemprop_where, "itemprop=\"where\">");
        map.put(md_greater_than, ">");
        map.put(md_less_than, "<");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry entry : entries) {
            str = str.replaceAll(entry.getValue().toString(), entry.getKey().toString());
        }
        Map<String, String> map2 = new LinkedHashMap<>();
        map2.put(md_itemprop_knowledge, "itemprop=\"knowledge\">");
        map2.put(md_itemprop_what, "itemprop=\"what\">");
        map2.put(md_itemprop_how, "itemprop=\"how\">");
        map2.put(md_itemprop_where, "itemprop=\"where\">");
        map2.put(md_div_last, "</div>");
        map2.put(md_div, "<div");
        Set<Map.Entry<String, String>> entries2 = map2.entrySet();
        for (Map.Entry entry : entries2) {
            str = str.replaceAll(entry.getKey().toString(), entry.getValue().toString());
        }
        System.out.println(str);

        //开始验证----------------------------------------
        System.out.println("---------------------------------------------------");
        //验证完成----------------------------------------

        Map<String, String> map3 = new LinkedHashMap<>();
        map3.put(md_greater_than, ">");
        map3.put(md_less_than, "<");
        Set<Map.Entry<String, String>> entries3 = map3.entrySet();
        for (Map.Entry entry : entries3) {
            str = str.replaceAll(entry.getKey().toString(), entry.getValue().toString());
        }

        System.out.println(str);

    }

    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

}
